package com.bingxue.edu.framework.config;

import com.bingxue.edu.common.config.NotifySmsProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.core.datainterface.SmsReadConfig;
import org.dromara.sms4j.provider.config.BaseConfig;
import org.dromara.sms4j.tencent.config.TencentConfig;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 短信配置动态加载
 * 当短信配置变更后，通过SmsFactory.reloadAll重新加载
 * @author panbo
 * @since 2024/8/29
 */
@Slf4j
@Order()
@Component
@RequiredArgsConstructor
public class SmsDynamicConfig implements SmsReadConfig {

    private final NotifySmsProperties notifySmsProperties;

    @Override
    public BaseConfig getSupplierConfig(String s) {
        return createConfig();
    }

    @Override
    public List<BaseConfig> getSupplierConfigList() {
        return List.of(createConfig());
    }

    private BaseConfig createConfig() {
        TencentConfig config = new TencentConfig();
        config.setConfigId(notifySmsProperties.getConfigId());
        config.setAccessKeyId(notifySmsProperties.getAccessKeyId());
        config.setAccessKeySecret(notifySmsProperties.getAccessKeySecret());
        config.setSdkAppId(notifySmsProperties.getSdkAppId());
        config.setSignature(notifySmsProperties.getSignature());

        log.info("加载短信配置成功");
        return config;
    }
}
